package com.swu.pad.comment.controller;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.w3c.dom.ls.LSInput;

import com.swu.pad.comment.dao.CommentQuestionDao;
import com.swu.pad.comment.entity.Comment;
import com.swu.pad.comment.entity.CommentQuestion;
import com.swu.pad.comment.entity.CommentTemplate;
import com.swu.pad.comment.entity.DataQuestion;
import com.swu.pad.comment.service.CommentService;
import com.swu.pad.comment.service.CommentTemplateService;
import com.swu.pad.system.entity.User;

import net.sf.ehcache.search.aggregator.Count;

@Controller
@RequestMapping(value = "/comment")
public class CommentController {

	@Autowired
	private CommentTemplateService commentTemplateService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private CommentQuestionDao commentQuestionDao ;

	@ResponseBody
	@RequestMapping(value = "/getComments")
	public List<Comment> getComments(String assignmentPadId) {
		return commentService.getComment(assignmentPadId);
	}
	
	
	


	@ResponseBody
	@RequestMapping(value = "/saveOrUpdateComment")
	public String saveOrUpdateComment(Comment comment) {
		commentService.saveOrUpateComment(comment);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/deleteComment")
	public String deleteComment(String commentId) {
		commentService.deleteComment(commentId);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getTemplates")
	public List<CommentTemplate> getTemplates() {
		return commentTemplateService.getTemplates();
	}

	@ResponseBody
	@RequestMapping(value = "/saveOrUpdateTemplate")
	public String saveOrUpdateTemplate(CommentTemplate commentTemplate) {
		commentTemplateService.saveOrUpateTemplate(commentTemplate);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/deleteTemplate")
	public String deleteTemplate(String templateId) {
		commentTemplateService.deleteTemplate(templateId);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getTemplateQuestions")
	public List<CommentQuestion> getTemplateQuestions(String templateId) {
	  return commentTemplateService.getTemplateQuestions(templateId);
	}
    
	@ResponseBody
	@RequestMapping(value = "/getReplyTemplateQuestions")
	public List<CommentQuestion> getReplyTemplateQuestions(String templateId) {
		 List<CommentQuestion> lista=commentTemplateService.getTemplateQuestions(templateId);
		 List<CommentQuestion> listb=commentTemplateService.getTemplateQuestions(templateId);
		 int max=0;
		 for (int i = 0; i < lista.size(); i++) {
		     	if(Integer.parseInt(lista.get(i).getSid())>=max){
		     		max=Integer.parseInt(lista.get(i).getSid());
		        }
		   }
		 List<String> questions=new ArrayList<String>();
		 for(int m=0;m<max;m++){
		 String sb="";
		 int count=0;
		 for (int i = 0; i <lista.size(); i++) {
			if(Integer.parseInt(lista.get(i).getSid())==(m+1)){
				if(count==0){
					sb=sb+lista.get(i).getQuestion();
					count++;
				}else{
					sb=sb+"&"+lista.get(i).getQuestion();
					count++;
				}
			 }
	       } 
		   questions.add(sb);
	     }
    //设置编号
    for (int i = 0; i <listb.size(); i++) {
		listb.get(i).setTitle(Integer.toString(i+1));
 	 }  
   //设置问题
    for (int j = 0; j < questions.size(); j++) {
	  for (int i = 0; i<listb.size();i++) {
		  if(Integer.parseInt(listb.get(i).getSid())==(j+1)){
			  listb.get(i).setQuestion(  listb.get(i).getQuestion()+"  "+"("+questions.get(j)+")");
		  }
		}
     }	 
       
  
		return listb;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/saveOrUpdateReplyQuestion")
	public String saveOrUpdateReplyQuestion(DataQuestion dataQuestion,@RequestParam("questionList[]")List<String> questionList) {
	   // commentTemplateService.saveOrUpateQuestion(commentQuestion
		     List<CommentQuestion> list=commentQuestionDao.findByTemplateId(dataQuestion.getTemplateId());
		     if(list.size()==0){//说明是新的模板 第一次添加 sid全部为1
			     for (int i = 0; i < questionList.size(); i++) {
				      CommentQuestion commentQuestion=new CommentQuestion();
				      commentQuestion.setScore(dataQuestion.getScore());
				      commentQuestion.setQuestion(questionList.get(i));
				      commentQuestion.setSid("1");
				      commentQuestion.setTitle(dataQuestion.getType());
				      commentQuestion.setTemplateId(dataQuestion.getTemplateId());
				      commentTemplateService.saveOrUpateQuestion(commentQuestion);
		           }	
		       }else{//说明添加过
		    	   int maxValue=0;
		    	 //  int count=0;
		    	   
		    	   for (int i = 0; i < list.size(); i++) {
					if( Integer.parseInt(list.get(i).getSid())>=maxValue){
					      maxValue=Integer.parseInt(list.get(i).getSid());
					 }
				   }
		    	  /* for (int i = 0; i < list.size(); i++) {
						if( Integer.parseInt(list.get(i).getSid())>=count){
						      count=Integer.parseInt(list.get(i).getSid());
						 }
					   }
		    	   */
		    	   maxValue=maxValue+1;
		    	   
		    	   for (int i = 0; i < questionList.size(); i++) {
					      CommentQuestion commentQuestion=new CommentQuestion();
					      commentQuestion.setScore(dataQuestion.getScore());
					      commentQuestion.setQuestion(questionList.get(i));
					      commentQuestion.setSid(String.valueOf(maxValue));
					      commentQuestion.setTemplateId(dataQuestion.getTemplateId());
					      commentQuestion.setTitle(dataQuestion.getType());
					      commentTemplateService.saveOrUpateQuestion(commentQuestion);
			           }	
		    	   
		       }
		  
	    	
		    	 return "success";
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveOrUpdateMutualQuestion")
	public String saveOrUpdateMutualQuestion(CommentQuestion commentQuestion) {
	             commentTemplateService.saveOrUpateQuestion(commentQuestion);
		    	 return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/deleteTemplateQuestion")
	public String deleteTemplateQuestion(String questionId) {
		commentTemplateService.deleteQuestion(questionId);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getCommentQuestions")
	public Map<String, Object> getCommentQuestions(String assignmentId, HttpSession session) {
		User user = (User) session.getAttribute("user");
		return commentTemplateService.getCommentQuestions(user.getId(), assignmentId);
	}
}
